package com.njtoyo.taxi.admin.rest.controller.business.operation_rule;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.njtoyo.taxi.admin.rest.presenter.business.operation_rule.InformationServiceFeePresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.InformationServiceFeeUpdateActiveWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.InformationServiceFeeWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.SimpleCallServiceRuleAddUpdateWrapper;
import com.njtoyo.taxi.admin.service.business.operation_rule.InformationServiceFeeService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@Api(value = "信息服务费", tags = "信息服务费")
@RequestMapping("business/operation-rule/information-service-fee")
public class InformationServiceFeeController {

    @Autowired
    private InformationServiceFeeService informationServiceFeeService;


    @GetMapping("/list")
    RestResult<IPage<InformationServiceFeePresenter>> list(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                                           @RequestParam(value = "size", required = false, defaultValue = "15") Integer size,
                                                           @RequestParam(value = "driverRegionName", required = false) String driverRegionName) {
        IPage<InformationServiceFeePresenter> page = informationServiceFeeService.getPage(current, size, driverRegionName);
        return RestResult.success(page);
    }


    @PostMapping("/add")
    RestResult add(@RequestBody InformationServiceFeeWrapper wrapper) {
        RestResult restResult = verityForm(wrapper);
        if (ObjectUtil.isNotNull(restResult)) {
            return restResult;

        }
        return informationServiceFeeService.add(wrapper);
    }


    @PostMapping("/update")
    RestResult update(@ApiIgnore @ModelAttribute("admin") AdminUser adminUser, @RequestBody InformationServiceFeeWrapper wrapper) {
        RestResult restResult = verityForm(wrapper);
        if (ObjectUtil.isNotNull(restResult)) {
            return restResult;
        }
        return informationServiceFeeService.update(adminUser,wrapper);
    }


    @GetMapping("/info/{id}")
    RestResult getInfoById(@PathVariable("id") Long id) {
        if (ObjectUtil.isNull(id)) {
            return RestResult.failed("缺少必要参数:ID");
        }
        return informationServiceFeeService.getInfoById(id);
    }

    @DeleteMapping("/delete/{id}")
    RestResult delete(@PathVariable("id") Long id) {
        if (ObjectUtil.isNull(id)) {
            return RestResult.failed("缺少必要参数:ID");
        }
        return informationServiceFeeService.deleteById(id);
    }


    private RestResult verityForm(InformationServiceFeeWrapper wrapper) {
        if (ObjectUtil.isNull(wrapper.getDriverRegionId())) {
            return RestResult.failed("请选择运营区");
        } else if (StrUtil.isBlank(wrapper.getStartTime()) || StrUtil.isBlank(wrapper.getEndTime())) {
            return RestResult.failed("请选择起始时间");
        } else if (ObjectUtil.isNull(wrapper.getRealtime().getPeakHour().getPerFee())) {
            return RestResult.failed("请填写[实时单-高峰时段]信息费占比");
        } else if (ObjectUtil.isNull(wrapper.getRealtime().getPeakHour().getMaxFee())) {
            return RestResult.failed("请填写[实时单-高峰时段]封顶金额");
        } else if (ObjectUtil.isNull(wrapper.getRealtime().getPeakHour().getMaxFee())) {
            return RestResult.failed("请填写[实时单-高峰时段]封顶金额");
        } else if (wrapper.getRealtime().getPeakHour().getPeriod().length == 0) {
            return RestResult.failed("请填写[实时单-高峰时段]时间段");
        } else if (ObjectUtil.isNull(wrapper.getRealtime().getNormalHour().getPerFee())) {
            return RestResult.failed("请填写[实时单-正常时段]信息费占比");
        } else if (ObjectUtil.isNull(wrapper.getRealtime().getNormalHour().getMaxFee())) {
            return RestResult.failed("请填写[实时单-正常时段]最大金额");
        } else if (ObjectUtil.isNull(wrapper.getAppointment().getFee())) {
            return RestResult.failed("请填写[预约单]固定费用");
        } else if (ObjectUtil.isNull(wrapper.getAppointment().getDriver_earning_ratio())) {
            return RestResult.failed("请填写[预约单]司机分成比例");
        }
        return null;
    }

    @PostMapping("/updateActive")
    public RestResult updateActive(@RequestBody InformationServiceFeeUpdateActiveWrapper wrapper) {
        return informationServiceFeeService.updateActive(wrapper);
    }

}
